/**
 * 
 */

function panelOperation(){

	/*1 conne radar*/
	$("#btnConnRadar").click(function() {
		let inputText = $("#inputText").val();
		let ipaddr = $("#ip1").ipv4_input("value");
		let ipport = 6000;
		//alert(ipaddr + " : " + ipport);
        
		$.ajax({
			url : 'ConnRadarServlet',
			data : {
				'ipaddr' : ipaddr,
				'ipport' : ipport
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("ConnRadar ......ok!  " + data);
			},
			error : function(data) {
				console.log("ConnRadar ......error!" + data);
			}
		});

	});	

	/*2 disconnect radar */
	$("#btnDisConnRadar").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		$.ajax({
			url : 'DisConnRadarServlet',
			data : {
				'ipaddr' : ipaddr
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("DisConnRadar ......ok!  " + data);
			},
			error : function(data) {
				console.log("DisConnRadar ......error!" + data);
			}
		});

	});		

	/*3 reset radar */
	$("#btnResetRadar").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		$.ajax({
			url : 'ResetRadarServlet',
			data : {
				'ipaddr' : ipaddr
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("ResetRadar ......ok!  " + data);
			},
			error : function(data) {
				console.log("ResetRadar ......error!" + data);
			}
		});

	});			
	
	/*4 clear clutter map */
	$("#btnClearClutterMap").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		$.ajax({
			url : 'ClearClutterMapServlet',
			data : {
				'ipaddr' : ipaddr
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("ClearClutterMap ......ok!  " + data);
				toastr["success"]("清除杂波成功！", "操作提示：");
			},
			error : function(data) {
				console.log("ClearClutterMap ......error!" + data);
				toastr["warning"]("清除杂波成功！", "操作提示：");
			}
		});

	});	
	
	/*5 preScan */
	$("#btnPreScan").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		let startAngle = 0;
		let endAngle = 180;
		let speed = 12;
		let scanCount = $("#preScanNum").val() || 10;
		$.ajax({
			url : 'PreScanServlet',
			data : {
				'ipaddr'     : ipaddr,
				'startAngle' : startAngle,
				'endAngle'   : endAngle,
				'speed'      : speed,
				'scanCount'  : scanCount
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("PreScan ......ok!  " + data);
				toastr["success"]("预扫描成功！", "操作提示：");
			},
			error : function(data) {
				console.log("PreScan ......error!" + data);
				toastr["warning"]("预扫描成功！", "操作提示：");
			}
		});

	});	

	/*6 Detect Scan */
	$("#btnDetectScan").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		let startAngle = 0;
		let endAngle = 180;
		let speed = 10;
		objInc = 0;//异物个数清空
		/*检测扫描前，清空列表*/
		$("#table-obj-tbody").html("");
		$.ajax({
			url : 'DetectScanServlet',
			data : {
				'ipaddr'     : ipaddr,
				'startAngle' : startAngle,
				'endAngle'   : endAngle,
				'speed'      : speed
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("DetectScan ......ok!  " + data);
				toastr["success"]("检测扫描成功！", "操作提示：");
			},
			error : function(data) {
				console.log("DetectScan ......error!" + data);
				toastr["warning"]("检测扫描成功！", "操作提示：");
			}
		});

	});	

	/*7 Mulit Reside */
	$("#btnMulitReside").click(function() {

		let scanNum = $("#stayPointScanNum").val();
		let tmpPara = {};
		// let tmpPara1 = {};
		// let tmpPara2 = {};
		// let tmpPara3 = {};
		// let tmpPara4 = {};
		// let tmpPara5 = {};
		// let tmpPara6 = {};
		// let tmpPara7 = {};
		// let tmpPara8 = {};
		// let tmpPara9 = {};
		// let tmpPara10 = {};
		console.log("scanNum: ",scanNum);
		if (scanNum < 1 || scanNum > 10)  {
			alert("请输入合适的驻留点数，驻留点数的范围是1-10！")
			return false;
		}

		let basicPara = {
			ip     : $("#ip1").ipv4_input("value"),
			startAngle : $("#stayPointStartAngle").val() || 0,
			endAngle   : $("#stayPointEndAngle").val() || 180,
			speed  : $("#stayPointScanSpeed").val() || 10,
			count      : $("#stayPointScanNum").val() || 10
		};

		// if(scanNum > 0)
		{
			tmpPara = {
				reside1 : {
					angle : $("#stayAngle1").val() || 15,
					time  : $("#stayTime1").val()  || 3000
				}
			};
			//console.log("0 tmpPara: ",tmpPara);
			//para = Object.assign({},basicPara,tmpPara);
			para = Object.assign({},basicPara,tmpPara);
		}

		// if(scanNum > 1)
		{
			tmpPara = {
				reside2 : {
					angle : $("#stayAngle2").val() || 30,
					time  : $("#stayTime2").val() || 3000
				}
			};
			//console.log("1 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}	

		// if(scanNum > 2)
		{
			tmpPara = {
				reside3 : {
					angle : $("#stayAngle3").val() || 45,
					time  : $("#stayTime3").val() || 3000
				}
			};
			//console.log("2 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}	
		// if(scanNum > 3)
		{
			tmpPara = {
				reside4 : {
					angle : $("#stayAngle4").val() || 60,
					time  : $("#stayTime4").val() || 3000
				}
			};
			//console.log("3 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}	
		// if(scanNum > 4)
		{
			tmpPara = {
				reside5 : {
					angle : $("#stayAngle5").val() || 75,
					time  : $("#stayTime5").val() || 3000
				}
			};
			//console.log("4 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}	
		// if(scanNum > 5)
		{
			tmpPara = {
				reside6 : {
					angle : $("#stayAngle6").val() || 90,
					time  : $("#stayTime6").val() || 3000
				}
			};
			//console.log("5 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}	
		// if(scanNum > 6)
		{
			tmpPara = {
				reside7 : {
					angle : $("#stayAngle7").val() || 105,
					time  : $("#stayTime7").val() || 3000
				}
			};
			//console.log("6 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}	
		// if(scanNum > 7)
		{
			tmpPara = {
				reside8 : {
					angle : $("#stayAngle8").val() || 120,
					time  : $("#stayTime8").val() || 3000
				}
			};
			//console.log("7 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}	
		// if(scanNum > 8)
		{
			tmpPara = {
				reside9 : {
					angle : $("#stayAngle9").val() || 135,
					time  : $("#stayTime9").val() || 3000
				}
			};
			//console.log("8 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}	
		// if(scanNum > 9)
		{
			tmpPara = {
				reside10 : {
					angle : $("#stayAngle10").val() || 150,
					time  : $("#stayTime10").val() || 3000
				}
			};
			//console.log("9 tmpPara: ",tmpPara);
			Object.assign(para,tmpPara);
		}							
		// let para = {...basicPara,...tmpPara1,...tmpPara2,...tmpPara3,...tmpPara4,...tmpPara5,...tmpPara6,...tmpPara7,...tmpPara8,...tmpPara9,...tmpPara10}
		console.log("para: ", para);
		let paraJson = JSON.stringify(para);
		console.log("paraJson: ", paraJson);
		$.ajax({
			url : 'MulitResideServlet',
			data : {
				'paraJson' : paraJson
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("MulitReside ......ok!  " + data);
				toastr["success"]("多点驻留成功！", "操作提示：");
			},
			error : function(data) {
				console.log("MulitReside ......error!" + data);
				toastr["warning"]("多点驻留成功！", "操作提示：");
			}
		});

	});	

	/*8 驻留检测 Reside Detect*/
	$("#btnResideDetect").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		let posAngle = 400;
		
		$.ajax({
			url : 'ResideDetectServlet',
			data : {
				'ipaddr'     : ipaddr,
				'posAngle' : posAngle || 40
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("Reside Detect ......ok!  " + data);
				toastr["success"]("驻留检测成功！", "操作提示：");
			},
			error : function(data) {
				console.log("Reside Detect ......error!" + data);
				toastr["warning"]("驻留检测失败！", "操作提示：");
			}
		});

	});	

	
	/*9 btnSwivelReset 设置转台水平零位*/
	$("#btnSwivelReset").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		$.ajax({
			url : 'SwivelResetServlet',
			data : {
				'ipaddr'     : ipaddr
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("Swivel Reset ......ok!  " + data);
				toastr["success"]("设置水平零位成功！", "操作提示：");
			},
			error : function(data) {
				console.log("Swivel Reset ......error!" + data);
				toastr["warning"]("设置水平零位失败！", "操作提示：");
			}
		});

	});	

	/*10 btnSwivelStop 紧急停止*/
	$("#btnSwivelStop").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		$.ajax({
			url : 'SwivelStopServlet',
			data : {
				'ipaddr'     : ipaddr
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("Swivel Reset ......ok!  " + data);
				toastr["success"]("设置紧急停止成功！", "操作提示：");
			},
			error : function(data) {
				console.log("Swivel Reset ......error!" + data);
				toastr["warning"]("设置紧急停止失败！", "操作提示：");
			}
		});

	});	


	/*11 Swivel Zero 机械归零*/
	$("#btnSwivelZero").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		$.ajax({
			url : 'SwivelZeroServlet',
			data : {
				'ipaddr'     : ipaddr
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("Swivel Zero ......ok!  " + data);
				toastr["success"]("设置机械归零成功！", "操作提示：");
			},
			error : function(data) {
				console.log("Swivel Zero  ......error!" + data);
				toastr["warning"]("设置机械归零失败！", "操作提示：");
			}
		});

	});	


	/* 12 转台（单点）驻留 SwivelReside*/
	$("#btnSwivelReside").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		let swiveResideSpd = $("#swiveResideSpeed").val();
		let swiveResidePos = Math.round($("#swiveResidePos").val());
		console.log("Swivel Reside Spd: " + swiveResideSpd + " Pos: " + swiveResidePos);
		$.ajax({
			url : 'SwivelResideServlet',
			data : {
				'ipaddr'     : ipaddr,
				'speed'      : swiveResideSpd || 10,
				'posAngle'   : swiveResidePos

			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("Swivel Reside  ......ok!  " + data);
				toastr["success"]("设置单点驻留成功！", "操作提示：");
			},
			error : function(data) {
				console.log("Swivel Reside  ......error!" + data);
				toastr["warning"]("设置单点驻留失败！", "操作提示：");
			}
		});

	});		

	/* 13 转台手动操作 SwivelControl */
	$("#btnSwivelControl").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		let checkValue = $("#swiveDirec").val(); 
		let direcFlag = 1;
        console.log("checkValue: ", checkValue);
		
        if (checkValue == "1")
        {
			//向左转
            direcFlag = 1;
        }
        else if (checkValue == "-1") 
        {
			//向右转
            direcFlag = -1;
        }
        else
        {
			//停止转
            direcFlag = 0;
        }  
        console.log("direcFlag: ",direcFlag);
		$.ajax({    
			url : 'SwivelControlServlet',
			data : {
				'ipaddr'     : ipaddr,
				'speed'      : $("#swiveSpeed").val() || 10,
				'direcFlag'  : direcFlag

			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("Swivel Control  ......ok!  " + data);
				toastr["success"]("设置转台手动成功！", "操作提示：");
			},
			error : function(data) {
				console.log("Swivel Control  ......error!" + data);
				toastr["warning"]("设置转台手动失败！", "操作提示：");
			}
		});

	});		

	/*14 配置IP  btnIP */
	$("#btnIP").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		let newIP = $("#ip2").ipv4_input("value");

		$.ajax({
			url : 'BtnIPServlet',
			data : {
				'ipaddr'     : ipaddr,
				'newIP'      : newIP || ipaddr
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("btnIP  ......ok!  " + data);
			},
			error : function(data) {
				console.log("btnIP  ......error!" + data);
			}
		});

	});		

	/*15 补光灯自动控制  btnAutoCtrlLed */
	$("#btnAutoCtrlLed").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		let ledCtrlCmd = 0;
		var spanText = $("#btnAutoCtrlLedText").html(); 
		if(spanText == '自动控制'){
			ledCtrlCmd = 0; //自动控制
			$("#btnAutoCtrlLedText").html('自动禁止'); 
		}else if(spanText == '自动禁止'){
			ledCtrlCmd = 2; //自动禁止
			$("#btnAutoCtrlLedText").html('自动控制'); 
		}
		$.ajax({
			url : 'LedCtrlServlet',
			data : {
				'ipaddr'     : ipaddr,
				'control'    : ledCtrlCmd
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("AutoCtrlLed  ......ok!  " + data);
				toastr["success"]("设置补光灯成功！", "操作提示：");
			},
			error : function(data) {
				console.log("AutoCtrlLed  ......error!" + data);
				toastr["warning"]("设置补光灯失败！", "操作提示：");
			}
		});

	});	

	/*16 补光灯手动控制  btnHandCtrlLed */
	$("#btnHandCtrlLed").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		let ledCtrlCmd = 0;
		var spanText = $("#btnHandCtrlLedText").html(); 
		console.log(`spanText: ${spanText}`);
		if(spanText == '手动控制'){
			ledCtrlCmd = 1; //手动控制
			$("#btnHandCtrlLedText").html('手动禁止'); 
		}else if(spanText == '手动禁止'){
			ledCtrlCmd = 3; //手动禁止
			$("#btnHandCtrlLedText").html('手动控制'); 
		}
		$.ajax({
			url : 'LedCtrlServlet',
			data : {
				'ipaddr'     : ipaddr,
				'control'    : ledCtrlCmd
			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("HandCtrlLed  ......ok!  " + data);
				toastr["success"]("设置补光灯成功！", "操作提示：");
			},
			error : function(data) {
				console.log("HandCtrlLed  ......error!" + data);
				toastr["warning"]("设置补光灯失败！", "操作提示：");
			}
		});

	});	


	/*17 设置门限 btnSetThreshold */
	$("#btnSetThreshold").click(function() {
		let ipaddr = $("#ip1").ipv4_input("value");
		$.ajax({
			url : 'SetThresholdServlet',
			data : {
				'ipaddr'     : ipaddr,
				'threshold'      : $("#inputThreshVal").val() || 6

			},
			dataType : 'text',
			type : 'get',
			success : function(data) {
				console.log("Set Threshold  ......ok!  " + data);
				toastr["success"]("设置门限成功！", "操作提示：");
			},
			error : function(data) {
				console.log("Set Threshold  ......error!" + data);
				toastr["warning"]("设置门限失败！", "操作提示：");
			}
		});

	});		

	/*18 雷达组清除杂波 btnGrpClearClutterMap */
	$("#btnGrpClearClutterMap").click(function() {
		//let ipaddr = $("#ip1").ipv4_input("value");
		let ipArray = [];
		let index = 0;
		$('tbody#table-sys-tbody tr').find('input[type=checkbox]:checked').each(function () {
			console.log($(this).parent().parent());
			let ip = $(this).parent().parent().attr('radarip');
			ipArray.push(ip);
			//ipArray[index++] = ip;
			
		});
		
		console.log("ipArray: " + ipArray);
		//var ipArrayJson = JSON.stringify(ipArray);	//转换前台数组
		var ipArrayJson = ipArray + "";
		console.log("ipArrayJson: " + ipArrayJson);
		
 		 $.ajax({
		 	url : 'GrpClearClutterMapServlet',
		 	data : {
		 		'ipArrayJson' : ipArrayJson
		 	},
		 	dataType : 'text',
		 	type : 'get',	
		 	//traditional : true,//防止深度序列化
		 	//cache : false,
		 	//async : false,
		 	success : function(data) {
		 		console.log("GrpClearClutterMap ......ok!  " + data);
				toastr["success"]("雷达组清除杂波成功！", "操作提示：");
		 	},
		 	error : function(data) {
		 		console.log("GrpClearClutterMap  ......error!" + JSON.stringify(data));
				toastr["warning"]("雷达组清除杂波失败！", "操作提示：");
		 	}
		 }); 

	});		

	/*19 雷达组预扫描 btnGrpPreScan */
	$("#btnGrpPreScan").click(function() {
		//let ipaddr = $("#ip1").ipv4_input("value");
		let ipArray = [];
		let startAngle = 0;
		let endAngle = 180;
		let speed = 10;
		let scanCount = $("#preScanNum").val() || 10;
		
		/*检测扫描前，清空列表*/
		$("#table-obj-tbody").html("");
		$('tbody#table-sys-tbody tr').find('input[type=checkbox]:checked').each(function () {
			console.log($(this).parent().parent());
			let ip = $(this).parent().parent().attr('radarip');
			ipArray.push(ip);
			
		});
		
		console.log("ipArray: " + ipArray);
		var ipArrayJson = ipArray + "";
		console.log("ipArrayJson: " + ipArrayJson);
		
 		 $.ajax({
		 	url : 'GrpPreScanServlet',
		 	data : {
		 		'ipArrayJson' : ipArrayJson,
				 'startAngle' : startAngle,
				 'endAngle'   : endAngle,
				 'speed'      : speed,
				 'scanCount'  : scanCount
		 	},
		 	dataType : 'text',
		 	type : 'get',
		 	success : function(data) {
		 		console.log("GrpPreScan ......ok!  " + data);
				toastr["success"]("雷达组预扫描成功！", "操作提示：");
		 	},
		 	error : function(data) {
		 		console.log("GrpPreScan  ......error!" + JSON.stringify(data));
				toastr["warning"]("雷达组预扫描失败！", "操作提示：");
		 	}
		 }); 

	});		

	/**20 雷达组检测扫描 btnGrpDetectScan */
	$("#btnGrpDetectScan").click(function() {
		let ipArray = [];
		let startAngle = 0;
		let endAngle = 180;
		let speed = 10;

		$('tbody#table-sys-tbody tr').find('input[type=checkbox]:checked').each(function () {
			console.log($(this).parent().parent());
			let ip = $(this).parent().parent().attr('radarip');
			ipArray.push(ip);
			
		});
		
		console.log("ipArray: " + ipArray);
		var ipArrayJson = ipArray + "";
		console.log("ipArrayJson: " + ipArrayJson);
		
 		 $.ajax({
		 	url : 'GrpDetectScanServlet',
		 	data : {
		 		'ipArrayJson' : ipArrayJson,
				'startAngle' : startAngle,
				'endAngle'   : endAngle,
				'speed'      : speed
		 	},
		 	dataType : 'text',
		 	type : 'get',
		 	success : function(data) {
		 		console.log("GrpDetectScan ......ok!  " + data);
				toastr["success"]("雷达组检测扫描成功！", "操作提示：");
		 	},
		 	error : function(data) {
		 		console.log("GrpDetectScan  ......error!" + JSON.stringify(data));
				toastr["warning"]("雷达组检测扫描失败！", "操作提示：");
		 	}
		 }); 

	});	

	/*21 雷达组复位 btnGrpResetRadar */
	$("#btnGrpResetRadar").click(function() {
		let ipArray = [];
		let index = 0;
		$('tbody#table-sys-tbody tr').find('input[type=checkbox]:checked').each(function () {
			console.log($(this).parent().parent());
			let ip = $(this).parent().parent().attr('radarip');
			ipArray.push(ip);
			
		});
		
		console.log("ipArray: " + ipArray);
		var ipArrayJson = ipArray + "";
		console.log("ipArrayJson: " + ipArrayJson);
		
 		 $.ajax({
		 	url : 'GrpResetRadarServlet',
		 	data : {
		 		'ipArrayJson' : ipArrayJson
		 	},
		 	dataType : 'text',
		 	type : 'get',
		 	success : function(data) {
		 		console.log("GrpResetRadar ......ok!  " + data);
				toastr["success"]("雷达组复位成功！", "操作提示：");
		 	},
		 	error : function(data) {
		 		console.log("GrpResetRadar  ......error!" + JSON.stringify(data));
				toastr["warning"]("雷达组复位失败！", "操作提示：");
		 	}
		 }); 

	});	




} 